//
//  NativeStoreContentCell.m
//  ebook
//
//  Created by mt on 17/1/10.
//  Copyright © 2017年 Moton. All rights reserved.
//

#import "NativeStoreContentCell.h"

@interface NativeStoreContentCell ()

@property (nonatomic, strong) UITextView *introView;//简介
@property (nonatomic, strong) UIButton *packBtn;  //展开
@property (nonatomic, strong) UILabel *checkMenuLabel;//查看目录
@property (nonatomic, strong) UIButton *packCoverBtn;
@property (nonatomic, strong) UIImageView *arrowImv;//箭头

@end

@implementation NativeStoreContentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self drawView];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)drawView {
    
    [self addSubview:self.introView];
    [self addSubview:self.packBtn];
    [self addSubview:self.checkMenuLabel];
    [self addSubview:self.packCoverBtn];
    [self addSubview:self.arrowImv];
}

- (UITextView *)introView {
    if (!_introView) {
        _introView = [[UITextView alloc] initWithFrame:CGRectMake(15, 0, MTScreenW-15*2, kNativeStoreContentCellHeight-15-60)];
        _introView.font = [UIFont systemFontOfSize:14.0];
        _introView.textColor = MTColor(149, 149, 149, 1.0);
    }
    return _introView;
}

- (UIButton *)packBtn {
    if (!_packBtn) {
        _packBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _packBtn.frame = CGRectMake(MTScreenW-15-50, kNativeStoreContentCellHeight-82, 50, 30);
        _packBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
        [_packBtn setTitle:@"展开↓" forState:UIControlStateNormal];
        [_packBtn setTitle:@"收起↑" forState:UIControlStateSelected];
        [_packBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [_packBtn addTarget:self action:@selector(packBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _packBtn;
}

- (UIButton *)packCoverBtn {
    if (!_packCoverBtn) {
        _packCoverBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _packCoverBtn.frame = CGRectMake(0, 0, MTScreenW, kNativeStoreContentCellHeight-40);
        _packCoverBtn.backgroundColor = [UIColor clearColor];
        [_packCoverBtn addTarget:self action:@selector(packBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _packCoverBtn;
}

- (UILabel *)checkMenuLabel {
    if (!_checkMenuLabel) {
        _checkMenuLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, kNativeStoreContentCellHeight-40, MTScreenW, 40)];
        _checkMenuLabel.backgroundColor = MTColor(250, 250, 250, 1);
        _checkMenuLabel.textAlignment = NSTextAlignmentCenter;
        _checkMenuLabel.textColor = [UIColor darkGrayColor];
    }
    return _checkMenuLabel;
}

- (UIImageView *)arrowImv {
    if (!_arrowImv) {
        _arrowImv = [[UIImageView alloc] initWithFrame:CGRectMake(MTScreenW-28, kNativeStoreContentCellHeight-40+(40-14)/2, 14, 14)];
        _arrowImv.image = [UIImage imageNamed:@"book_arrow"];
    }
    return _arrowImv;
}
- (void)setModel:(NativeOrderlistModel *)model {

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:model.introduction];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [model.introduction length])];
    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.0] range:NSMakeRange(0, model.introduction.length)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:MTColor(149, 149, 149, 1.0) range:NSMakeRange(0, model.introduction.length)];
    self.introView.attributedText = attributedString;
    
    NSString *lastupdatetime = [NSString stringWithFormat:@"查看目录 更新于%@", model.lastupdate];
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:lastupdatetime];//@"查看目录 更新于2016-09-08 00:40:15"
    [attStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, 4)];
    [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:NSMakeRange(0, attStr.length)];
    self.checkMenuLabel.attributedText = attStr;
}

- (void)setRowHeight:(CGFloat)rowHeight {
    
    if (rowHeight>kNativeStoreContentCellHeight) {
        self.introView.height = rowHeight-70;
        self.checkMenuLabel.y = rowHeight-40;
        self.packBtn.y = rowHeight-75;
        self.packCoverBtn.height = rowHeight-40;
        self.arrowImv.y = self.checkMenuLabel.y+(40-14)/2;
        
    } else {
        self.introView.height = kNativeStoreContentCellHeight-70;
        self.checkMenuLabel.y = kNativeStoreContentCellHeight-40;
        self.packBtn.y = kNativeStoreContentCellHeight-75;
        self.packCoverBtn.height = kNativeStoreContentCellHeight-40;
        self.arrowImv.y = self.checkMenuLabel.y+(40-14)/2;
    }
}

- (void)setIsHiddenPackBtn:(BOOL)isHiddenPackBtn {
    
    self.packBtn.hidden = isHiddenPackBtn;
    self.packCoverBtn.hidden = isHiddenPackBtn;
}

- (void)packBtnClick:(UIButton *)btn {

    self.packBtn.selected = !self.packBtn.selected;
    
    MTLog(@"btn.selected:%d", btn.selected);
    if ([self.delegate respondsToSelector:@selector(nativeStoreContentCell:isCellFold:)]) {
        [self.delegate nativeStoreContentCell:self isCellFold:self.packBtn.selected];
    }
}

@end
